
package com.bei_en.teacher.management.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bei_en.teacher.management.controller.BaseController;
import com.bei_en.teacher.management.model.system.condition.SystemLogsCondition;
import com.bei_en.teacher.management.model.system.vo.SystemLogsVo;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.service.system.SystemLogsService;
import com.bei_en.teacher.management.web.MyJsonFailResult;
import com.bei_en.teacher.management.web.MyJsonResult;
import com.bei_en.teacher.management.web.MyJsonSuccessResult;

/**
 * 系统日志
 * @author Longqs
 * @date 2016-6-15 11:43:10
 */
@Controller
@RequiresRoles("admin")
@RequestMapping("/admin")
public class SystemLogsController extends BaseController {

    @Autowired
    SystemLogsService systemLogsService;

    private void addSharedAttr(Model model) {

	model.addAttribute("userMap", systemUserMap());
	model.addAttribute("typeMap", gson.fromJson("{1:'写入',2:'更新',3:'删除'}", Map.class));

    }

    @RequestMapping("/systemLogs")
    public String systemLogs(Model model, SystemLogsCondition vo, HttpServletResponse resp) {

	List<SystemLogsVo> list = new ArrayList<SystemLogsVo>();
	try {
	    list = systemLogsService.findSystemLogsPage(vo);
	} catch (Exception e) {
	    e.printStackTrace();
	    writeJson(resp, new MyJsonFailResult(e.getMessage()));
	    return null;
	}

	model.addAttribute("list", list);
	addSharedAttr(model);
	model.addAttribute("vo", vo);

	return "/admin/system/logsList.html";
    }

    @RequestMapping("/systemLogsDel")
    @ResponseBody
    public MyJsonResult systemLogsDel(Model model, int[] delids, HttpSession session) {

	try {
	    for (int i : delids) {
		systemLogsService.delete(i);

		SystemUserVo obj = getLoginUser(session);
		String content = "删除后台系统日志id：" + i;
		logsInsert(obj.getId(), content, 3);

	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return new MyJsonFailResult(e.getMessage());
	}
	return new MyJsonSuccessResult();
    }

}
